// 获取时间格式
export function formatTime(number,format) {  
  
  var formateArr  = ['Y','M','D','h','m','s'];  
  var returnArr   = [];  
  
  var date = new Date(number * 1000);  
  returnArr.push(date.getFullYear());  
  returnArr.push(this.formatNumber(date.getMonth() + 1));  
  returnArr.push(this.formatNumber(date.getDate()));  
  
  returnArr.push(this.formatNumber(date.getHours()));  
  returnArr.push(this.formatNumber(date.getMinutes()));  
  returnArr.push(this.formatNumber(date.getSeconds()));  
  
  for (var i in returnArr)  
  {  
    format = format.replace(formateArr[i], returnArr[i]);  
  }  
  return format;  
} 

//数据转化  
function formatNumber(n) {  
  n = n.toString()  
  return n[1] ? n : '0' + n  
} 

//时间戳转换时间  
export function toData(number) {
  var n = number * 1000;
  var date = new Date(n);
  var Y = date.getFullYear() + '/';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  return (Y + M + D)
}

export function dateFtt(fmt, date) { //author: meizz   
  var o = {
    "M+": date.getMonth() + 1, //月份   
    "d+": date.getDate(), //日   
    "h+": date.getHours(), //小时   
    "m+": date.getMinutes(), //分   
    "s+": date.getSeconds(), //秒   
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
    "S": date.getMilliseconds() //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

// 秒数获取时间格式
export function formatSecondTime(s) {
  let date = new Date(s * 1000)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 订单时间显示格式
export function getDateDiff(s) {
  // 订单时间
  let date = new Date(s * 1000)
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()
  // 当前时间
  const nowDate = new Date()
  const nowYear = nowDate.getFullYear()
  const nowMonth = nowDate.getMonth() + 1
  const nowDay = nowDate.getDate()
  // 如果同年同月同日
  if (year === nowYear && month === nowMonth && day === nowDay) {
    return '今天 ' + [hour, minute].map(formatNumber).join(':')
  } else {
    return `${year}年${month}月${day}日`
  }
}



//日期转成时间戳
export function gettime(time) {
  // var date=new Date('2014-04-23 18:55:49:121');
  console.log(time, 90)
  var date = new Date(time);
  // var date = new Date(time.replace(/-/g, '/'));
  var time1 = date.getTime();
  return time1
}
// 订单排序
export function orderSort(obj1, obj2) {
  let a = obj1.result.result[0].baseInfo.createTime
  let b = obj2.result.result[0].baseInfo.createTime
  if (a < b) {
    return 1
  } else if (a > b) {
    return -1
  } else {
    return 0
  }
}





// 1位数前面补0
export function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 获取月份的天数
export function getMonthDate(m) {
  let d = new Date()
  d.setMonth(m + 1)
  d.setDate(1)
  d.setMonth(m)
  d.setDate(0)
  let date = d.getDate() || 0
  return date
}

// 获取周数
export function getWeek(date, s) {
  let day = date.getDay()
  let week = ['日', '一', '二', '三', '四', '五', '六']
  let str = (s || '星期') + week[day]
  return str
}

// 判断对象是否为空
export function isEmptyObject(obj) {
  for (let key in obj) return false
  return true
}

// 提示
export function showToast(title, icon, duration) {
  wx.showToast({
    title: title,
    icon: icon || 'loading',
    duration: duration || 60000
  })
}

// 提示错误弹窗
export function showError(title, content) {
  wx.showModal({
    title: title || '提示',
    content,
    showCancel: false
  })
}

// 去掉空格和转换成字符串
export function getString(obj) {
  for (let k in obj) {
    obj[k] = obj[k].toString().trim()
  }
  return obj
}


export function parseXML(ret){
  var xmlDoc = null;
  try //Internet Explorer
  {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(ret);
  }
  catch (e) {
    try //Firefox, Mozilla, Opera, etc.
    {
      parser = new DOMParser();
      xmlDoc = parser.parseFromString(ret, "text/xml");
    }
    catch (e) { 
      // alert(e.message) 
      }
  }
  return xmlDoc;
}

// 提取富文本中的图片地址src
export function getPicSrc(_data) {
  // 获取图片
  let imgReg = /<img.*?(?:>|\/>)/gi;
  // //匹配src属性
  let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/gi;
  let arr = _data.intro.match(imgReg)||[];
  arr = arr.map(item=>{
    item = item.match(srcReg);
    return item[0].replace(/src=/i, '').slice(1,-1)
  })
  return arr
}

// 校验手机号是否合法
const checkPhone = _phone=>new RegExp(/0?(13|14|15|16|17|18|19)[0-9]{9}/).test(_phone);

const ckg = ( current='', urls=[] ) => {
  wx.previewImage({ current, urls })
}

/**
 * @todo 待完善 19-05-18
 * @arg {Object} object  - 监听的对象
 * @arg {String} element - 监听的元素
 * @arg {callback}  callback  - 回调函数
 */
const Listener = ( object, element, callback ) => {
  // console.log( Object.prototype.toString.call(element).match(/\[object (.*?)\]/)[1] )
  Object.defineProperties( object,{
    [element]:{
      get: function(val) { return this.value },
      set: function(val) { callback(val); this.value = val }
    }
  }
  )
}

const compatibility = ( ) => {
  const app = getApp()
  wx.getSystemInfo({
    success: function(res) {
      // console.log(res, res.model, app)
        var model = res.model
        if (model.search('iPhone X') != -1){
          app.globalData.compatibility.isIpx = true;
        }else{
          app.globalData.compatibility.isIpx = false;
        }
    }
  })
}

const mandatory = (msg = '必填参数缺失！') => {
  throw new Error(msg);
}

const msg = (msg = '请求有误') => {
  wx.showToast({
    title: msg,
    icon: 'none',
    duration: 2000
  })
}

const wechat = class Wechat {
  static api(name = mandatory('缺失wx的Api名称字段'), opts = {}) {
    return new Promise((success, fail) => {
      let obj = { ...opts, ...{ success, fail } };
      wx[name](obj);
    });
  };
  /**
   * @param {*} _fn 传入一个方法函数
   * @demo 
   * let a = await wechat.pro((res)=>{
   *  操作过程。。。
   *  res('输出结果')
   * })
   */
  static pro(_fn) {
    return new Promise((resolve, reject) => {
      _fn(resolve)
    });
  };
}

const rate = wx.getSystemInfoSync().windowWidth / 750;
const rpx2px = rpx => rate * rpx;

export { Listener, checkPhone, ckg, compatibility, msg, wechat, rpx2px }